<?php
declare (strict_types = 1);

namespace app;
use app\model\Collect;
use think\Model;
use Yurun\Util\Chinese;
use Yurun\Util\Chinese\Pinyin;


/**
 * 基础模型类
 */
 class BaseModel extends Model
{
//     protected $append = ['date_time','ago_time'];
     protected $hidden = ['update_time','delete_time'];
     //protected $append = ['ids'];

     /**
      * 搜索器
      * title多字段搜索器
      * like查询可以没有empty
      */
    public function getIdsAttr($value, $data)
    {
        return $data['id'];
    }

    public function searchListRowsAttr($query,$value,$data){

    }
    public function searchTokenAttr($query,$value,$data){

    }
     public function searchRandAttr(){}
     public function searchPageAttr(){}


     public function searchTitleAttr($query, $value, $data)
     {
         if (!empty($value)) {
             $query->where('title', 'like', '%' . $value . '%');
         }
     }

     public function searchProvAttr($query, $value, $data)
     {
         if (!empty($value)) {
             $query->where('prov', 'like', '%' . $value . '%');
         }
     }
     public function searchCityAttr($query, $value, $data)
     {
         if (!empty($value)) {
             $query->where('city', 'like', '%' . $value . '%');
         }
     }
     public function searchAreaAttr($query, $value, $data)
     {
         if (!empty($value)) {
             $query->where('area', 'like', '%' . $value . '%');
         }
     }

     public function searchCatIdAttr($query, $value, $data)
     {
         if (!empty($value)) {
             $query->where('cat_id', '=',$value);
         }
     }

     public function searchCatId2Attr($query, $value, $data)
     {
         if (!empty($value)) {
             $query->where('cat_id2', '=',$value);
         }
     }

     public function searchCatId3Attr($query, $value, $data)
     {
         if (!empty($value)) {
             $query->where('cat_id3', '=',$value);
         }
     }


     public function searchCreateTimeAttr($query, $value, $data)
     {
         if (!empty($value)) {
             $start_time = explode(' / ', $value)[0];
             $end_time = date('Y-m-d', strtotime(explode(' / ', $value)[1]) + 3600 * 24);
             $query->where('create_time', 'between', [$start_time, $end_time]);
         }
     }

     public function searchParentIdAttr($query, $value, $data)
     {
         if (!empty($value)) {
             $query->where('parent_id', '=', $value);
         }
     }



     /**
      * 获取收藏状态
      */
     protected function getCollectStatusAttr($value, $data)
     {

         $collect = Collect::where([
             ['member_id', '=', request()->member_id],
             ['controller', '=', request()->controller()],
             ['view_id', '=', $data['id']],
         ])->find();
         if (!empty($collect)) {
             return 2;
         } else {
             return 1;
         }
     }

     /**
      * 获取下级分类的总数量
      */
     protected function getSonCountAttr($value, $data)
     {
        return self::where('parent_id',$data['id'])->count();
     }

     /**
      * 生成二维码
      */
     protected function getQrcodeAttr($value, $data)
     {

         return qrcode(url('mobile/Index/index',['unit_id'=>$data['id']],false,true)->build(),'unit/'.$data['id'].'.png')['url'].'?rand='.mt_rand(1,100);
     }

    /**
     * 时间转换
     */
     protected function getAgoTimeAttr($value,$data)
     {
         return AgoTime($data['create_time']);
     }
     protected function getDateTimeAttr($value,$data)
     {
         return date('Y-m-d',strtotime($data['create_time']));
     }

     /**
      * 多个图片获取器
      */
     protected function getMultiPicAttr($value, $data)
     {
         return !empty($value) ? unserialize($value) : [];
     }

     /**
      * @param $value
      * @param $data
      * @return string
      *
      */
     protected function setMultiPicAttr($value, $data)
     {
         if (is_string($value)) $value = explode(',', $value);//把前台的转换成和后台一样的数组;

         $picarr = $value;
         if (!empty($picarr) && is_array($picarr)) {
             $info = input('info/a');
             $show = input('show/a');
             $arr = array();
             foreach ($picarr as $key => $v) {
                 if($key == 0){
                    // $this->set('single_pic',$v);
                 }
                 $row['img'] = $v;
                 if (!empty($info)){
                     $row['info'] = $info[$key];
                 }else{
                     $row['info'] = $key+1;
                 }
                 $row['show'] = $show[$key];
                 $arr[] = $row;
             }

             return serialize($arr);
         } else {
             return '';
         }
     }

    protected function setUploadManyAttr($value, $data)
    {
        if (is_string($value)) $value = explode(',', $value);//把前台的转换成和后台一样的数组;

        $picarr = $value;
        if (!empty($picarr) && is_array($picarr)) {
            $info = input('info/a');
            $show = input('show/a');
            $arr = array();
            foreach ($picarr as $key => $v) {
                if($key == 0){
                   // $this->set('single_pic',$v);
                }
                $row['img'] = $v;
                if (!empty($info)){
                    $row['info'] = $info[$key];
                }else{
                    $row['info'] = $key+1;
                }
                $row['show'] = $show[$key];
                $arr[] = $row;
            }

            return serialize($arr);
        } else {
            return '';
        }
    }

    protected function getUploadManyAttr($value, $data)
    {
        return !empty($value) ? unserialize($value) : [];
    }

    protected function getShareUrlAttr($value, $data)
    {
        return url('index/view', ['controller' => request()->controller(), 'view_id' => $data['id']], false, true)->build();
    }

    /*
     * 设置拼音
     */
    protected function setPinyinAttr($value, $data)
    {
         //Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_FIRST, '', false)['pinyinFirst'][0]
    }









 }